package cn.microven.mindmap.components.part;

import cn.microven.mindmap.components.line.MapLine;
import cn.microven.mindmap.components.node.AbstractMapNode;
import cn.microven.mindmap.oprations.AddLeafOperation;
import cn.microven.mindmap.struture.MindMap;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.util.Pair;

public class LeftMapPart extends MapPart {

    /**
     * 构造方法
     *
     * @param root 根节点
     */
    public LeftMapPart(AbstractMapNode root) {
        super(root);
    }

    /**
     * 初始化布局
     */
    @Override
    protected void initLayout() {
        this.getChildren().add(vBox);
        this.getChildren().add(linesBox);
        this.getChildren().add(root);
        this.setAlignment(Pos.CENTER_RIGHT);
        vBox.setAlignment(Pos.CENTER_RIGHT);
    }

    /**
     * 连接点绑定
     */
    @Override
    protected void bindIntersection(){
        //连接点属性绑定
        leftIntersectionY = this.root.layoutYProperty().add(this.root.heightProperty().divide(2));
        rightIntersectionY =  this.layoutYProperty().add(this.root.layoutYProperty()).add(this.root.heightProperty().divide(2));
    }

    @Override
    public void addLeaf() {
        LeftMapPart lmp = new LeftMapPart(MindMap.getInstance().getDefaultNode().clone());
        this.addLeaf(lmp);
    }

    /**
     * 添加子节点
     * 子节点必须是已经初始化的 MapPart 类型
     *
     * @param nodePart 子节点
     */
    @Override
    public void addLeaf(Part nodePart) {
        //添加节点
        LeftMapPart node = (LeftMapPart) nodePart;
//        vBox.getChildren().add(node);
        AddLeafOperation alo = new AddLeafOperation(this, leavesProperty(), vBox,linesBox,new Pair<>(connectLine(node), node));
        alo.start();
    }

    @Override
    public String getType() {
        return "LeftMapPart";
    }

    @Override
    public MapLine connectLine(Part node) {
        //初始化连线连线
        MapLine line = new MapLine();
        line.getQuad().controlXProperty().bind(line.startXProperty().add(line.endXProperty().divide(10)));

        //设置开始横坐标
        line.setEndX(2);
        line.startXProperty().bind(linesBox.widthProperty().subtract(2));

        //绑定连线的起点和终点，与节点的连接点相对应
        line.startYProperty().bind(this.getLeftIntersectionY());
        line.endYProperty().bind(node.getRightIntersectionY());

        return line;
    }

    /**
     * 转化为左布局
     * @return 转化为左布局后的 LeftMapPart
     */
    public RightMapPart castToRight() {
        return castLeafToRight(this);
    }

    /**
     * 遍历树结构并更换节点布局
     * @param part 当前节点
     * @return 转化结果
     */
    protected RightMapPart castLeafToRight(LeftMapPart part) {
        RightMapPart right = new RightMapPart(this.root);
        ObservableList<Pair<MapLine, Part>> children = part.getLeaves();

        for (Pair<MapLine, Part> child : children) {
            MapPart mpChild = (LeftMapPart) child.getValue();
            right.addLeaf(mpChild.castToRight());
        }

        return right;
    }

    @Override
    public LeftMapPart castToLeft() {
        return this;
    }
}
